<template>
    <div id="newsList">
        <!-- 页面头部 -->
        <HomePageHead></HomePageHead>
        <!-- 导航栏 -->
        <HomePageNavigation1></HomePageNavigation1>
        <!-- 列表页广告一 -->
        <HomeTopTen :imgurl="adImg1" v-if="adImg1"></HomeTopTen>
        <!-- 二级标题-->
        <HomeListSecondaryHeading v-if="parent_name != ''" :name="parent_name" :nav="secondNav" :pinyin="parent_pinyin">
        </HomeListSecondaryHeading>
        <!-- 资讯列表 -->
        <div class="newsList">
            <div class="inner">
                <div class="innerLeft">
                    <!-- 面包屑导航 -->
                    <div class="breadcrumb">
                        <div class="inner">
                            <span class="location">当前位置:</span>
                            <el-breadcrumb :separator-icon="ArrowRight">
                                <el-breadcrumb-item>
                                    <NuxtLink to="/">首页</NuxtLink>
                                </el-breadcrumb-item>
                                <el-breadcrumb-item v-if="parent_name != ''">
                                    <NuxtLink :to="`/${parent_pinyin}/index.html`"> {{ parent_name }}</NuxtLink>
                                </el-breadcrumb-item>
                                <el-breadcrumb-item>{{ name }}</el-breadcrumb-item>
                            </el-breadcrumb>
                        </div>
                    </div>
                    <ul class="list">
                        <li v-for="(item, index) in newsList" :key="index">
                            <NuxtLink :href="getLinkPathDetail(item)" :title="item.alias">
                                <span class="listTitle">{{ item.title }}</span>
                                <span class="time">{{ getTime(item.updated_at, 'month', 1) }}</span>
                            </NuxtLink>
                        </li>
                    </ul>
                    <!-- 分页器 -->
                    <div class="pagination" v-if="total > 0">
                        <el-pagination size="small" background layout="prev, pager, next" :total="total" class="mt-4"
                            :page-size="pageSize" :current-page="pageNum" prev-text="上一页" next-text="下一页"
                            @current-change="changePage" />
                    </div>
                </div>
                <div class="innerRight">
                    <DetailHotNews></DetailHotNews>
                    <DetailHotNews2></DetailHotNews2>
                </div>
            </div>
        </div>
        <!-- 列表页广告二 -->
        <HomeTopTen :imgurl="adImg2" v-if="adImg2"></HomeTopTen>
        <!-- 页面底部 -->
        <HomeFoot1></HomeFoot1>
    </div>
</template>

<script setup>
//1.页面必备依赖 start ---------------------------------------->
import { ElBreadcrumb, ElBreadcrumbItem, ElPagination } from 'element-plus';
import { ArrowRight } from '@element-plus/icons-vue';
import { ref, onMounted } from 'vue';
//1.页面必备依赖 end ---------------------------------------->

//2.页面路径 start ---------------------------------------->
const route = useRoute();
let articleId = 0; //获取哪个导航下的列表
//2.1 获得当前的完整路径
const targetSegment = getRoutePath(1);
//通过导航路径反向查询导航id
const getRouteId = await requestDataPromise('/web/getWebsiteRoute', {
    method: 'GET',
    query: {
        'pinyin': targetSegment,
    },
});
if (getRouteId.code == 200) {
    articleId = getRouteId.data.category_id
} else {
    console.log("错误位置：获得页面路径")
}
//2.页面路径 end ---------------------------------------->

//3.页面数据 start ---------------------------------------->
let pageNum = ref(2); //当前页码
pageNum.value = parseInt(route.params.id);//路由中传递的分页页码
let total = ref(1); //总条数
let pageSize = ref(20); //每页条数

//3.1 新闻列表
const newsList = ref([]);
let newslists = async () => {
    const listData = await requestDataPromise('/web/getWebsiteArticleList', {
        method: 'GET',
        query: {
            'page': pageNum.value,
            'pageSize': pageSize.value,
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        newsList.value = listData.data.rows;
        total.value = listData.data.count;

    } else {
        console.log("错误位置：获取新闻列表")
    }
}
//获得列表
newslists();

//3.2 分页事件
let changePage = (value) => {
    console.log("当前页码", value);
    navigateTo(`/${targetSegment}/list-${value}.html`)
}
//3.页面数据 end ---------------------------------------->

//4.面包屑 start ---------------------------------------->
const name = ref('')
//4.1 当前频道名称
let getPageName = async () => {
    const pageName = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (pageName.code == 200) {
        name.value = pageName.data.alias
    } else {
        console.log("错误位置：获取当前频道名称", pageName.message)
    }
}
getPageName();

//4.2 查询是否含有父级导航
const parent_name = ref([]);
const parent_id = ref([]);
const parent_pinyin = ref("");
let getParentNav = async () => {
    const listData = await requestDataPromise('/web/getOneWebsiteCategory', {
        method: 'GET',
        query: {
            'catid': articleId
        },
    });
    if (listData.code == 200) {
        parent_name.value = listData.data.parent_name;
        parent_id.value = listData.data.parent_id;
        parent_pinyin.value = listData.data.parent_pinyin;
    } else {
        console.log("错误位置：查询父级导航信息")
    }
    getSecondNav();
}
getParentNav();

//4.3 获取二级栏目列表
const secondNav = ref([]);
let getSecondNav = async () => {
    const listData = await requestDataPromise('/web/getWebsiteModelCategory', {
        method: 'GET',
        query: {
            'placeid': 1,
            'pid': parent_id.value,
            'num': 8,
        },
    });
    console.log('listData', listData);

    if (listData.code == 200) {
        secondNav.value = listData.data
    } else {
        console.log("错误位置：获得二级栏目列表")
    }
}
//4.面包屑 end ---------------------------------------->

//5.设置seo信息 start---------------------------------------->
const setData = await requestDataPromise('/web/getWebsiteCategoryHead', {
    method: 'GET',
    query: {
        'catid': articleId
    },
});

if (setData.code == 200) {
    let seoTitle = setData.data.seo_title;
    let seoDescription = setData.data.seo_description;
    let seoKeywords = setData.data.seo_keywords;
    let seoSuffix = setData.data.suffix;
    let seoName = setData.data.website_name;

    useSeoMeta({
        title: seoTitle + "_" + seoName + "_" + seoSuffix,
        meta: [
            { name: 'keywords', content: seoKeywords + "_" + seoName + "_" + seoSuffix, tagPriority: 10 },
            { name: 'description', content: seoDescription + "_" + seoName + "_" + seoSuffix, tagPriority: 10 }
        ]
    });
} else {
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
    console.log("错误位置：设置列表页面SEO数据")
    console.log("后端错误反馈：", setData.message)
    console.log("SSR waring ---------- SSR waring ---------- SSR waring ---------->")
}
//5.设置seo信息 end---------------------------------------->

//6.广告 start---------------------------------------->
let adImg1 = ref({});
let adImg2 = ref({});

onMounted(async () => {

    //从客户端获取行政职能部门 加快打开速度
    const { $webUrl, $CwebUrl } = useNuxtApp();
    //广告1
    let url = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmw_list_0001`
    const responseAd1 = await fetch(url, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd1 = await responseAd1.json();
    adImg1.value = resultAd1.data[0];

    //广告2
    let url2 = `${$webUrl}/web/getWebsiteAdvertisement?ad_tag=nmw_list_0002`
    const responseAd2 = await fetch(url2, {
        headers: {
            'Content-Type': 'application/json',
            'Userurl': $CwebUrl,
            'Origin': $CwebUrl
        }
    });
    const resultAd2 = await responseAd2.json();
    adImg2.value = resultAd2.data[0];

})
//6.广告 end---------------------------------------->
</script>

<style lang="less" scoped>
@import url('@/assets/css/list.less');
</style>